#include <assert.h>

#include "remez.h"
#include "remez_log.h"

using namespace mpfr;


// log1p(x)= log(1+x)
mpreal log1p_func(mpreal x)
{
    return log1p(x);
}

// evaluatePolynomial(x)  = coeffs[0] + coeffs[1]*x + coeffs[2]*x^2 + coeffs[3]*x^3 + coeffs[4]*x^4 + coeffs[5]*x^5
// 返回　my_log1p(x) -log1p(x)的导数， f′(x)- log1p′(x)
mpreal log1p_error_derivative(const std::vector<mpreal> &coeffs, mpreal x0,  int degree) // degree为多项式的阶数
{
    assert(degree+1<=coeffs.size());

    // coeffs=[c0,c1,c2,c3,c4,c5]
    // f(x)= c0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4 + c5*x^5
    // f′(x)=c1 + 2*c2*x + 3*c3*x^2 + 4*c4*x^3+ 5*c5*x^4
    mpreal r= polynomial_derivative( coeffs, x0, degree);
    
    mpreal one= mpreal("1");
    return r- one/(one+x0);
}

// 计算my_log1p(x)-log1p_func(x),即计算　mylog　在x点的误差
mpreal log1p_get_error_at_x( const std::vector<mpreal> &coeffs, mpreal x, int degree) // degree为多项式的阶数
{
    mpreal r = evaluatePolynomial(coeffs,x,degree);
    return r-log1p_func(x);
}
